package mobileann.mafamily.utils;

import java.io.File;

import mobileann.mafamily.act.main.FS;
import android.graphics.Bitmap;
import android.widget.ImageView;

import com.mobileann.mafamily.R;
import com.mofind.java.utils.FileUtils;
import com.nostra13.universalimageloader.cache.disc.impl.UnlimitedDiskCache;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;
import com.nostra13.universalimageloader.core.listener.ImageLoadingProgressListener;
import com.nostra13.universalimageloader.utils.StorageUtils;

public class ImageLoaderUtil {

	private static ImageLoaderUtil instance = null;
//	private static ImageLoaderUtil instanceHelp = null;

//	private String filePath;

	public static ImageLoaderUtil getInstance() {
		if (instance == null) {
			instance = new ImageLoaderUtil();
		}
		return instance;
	}

//	public static ImageLoaderUtil getInstanceHelp() {
//		if (instanceHelp == null) {
//			instanceHelp = new ImageLoaderUtil(FileUtils.MABABY_IMAGECCACHE);
//		}
//		return instanceHelp;
//	}

	private ImageLoaderUtil() {
		setImageConf();
	}

//	private ImageLoaderUtil(String filePath) {
//		setImageConf(filePath);
//	}
//
//	private void setImageConf(String filePath) {
//		File cacheDir = StorageUtils.getOwnCacheDirectory(FS.getInstance(), filePath);
//		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
//		.showImageForEmptyUri(R.drawable.icon)
//		.bitmapConfig(Bitmap.Config.ARGB_4444)
//		.showImageOnFail(R.drawable.icon).cacheInMemory(true)
//		.delayBeforeLoading(8/*00*/).cacheOnDisc(true).build();
//		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
//				FS.getInstance()).defaultDisplayImageOptions(defaultOptions)
//				.discCacheSize(50 * 1024 * 1024).discCacheFileCount(1000)
//				.discCache(new UnlimitedDiscCache(cacheDir)).build();
//		ImageLoader.getInstance().init(config);
//	}

	private void setImageConf() {
		String path = FileUtils.MABABY_IMAGECCACHE2;
		File cacheDir = StorageUtils.getOwnCacheDirectory(FS.getInstance(), path);
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.icon)
				.bitmapConfig(Bitmap.Config.ARGB_4444)
				.showImageOnFail(R.drawable.icon).cacheInMemory(true)
				.delayBeforeLoading(8/*00*/).cacheOnDisc(true).build();

		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				FS.getInstance()).defaultDisplayImageOptions(defaultOptions)
				.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(1000)
				.diskCache(new UnlimitedDiskCache(cacheDir)).build();
		ImageLoader.getInstance().init(config);
//		ImageLoader.getInstance().clearDiskCache();
//		ImageLoader.getInstance().clearMemoryCache();
	}

	public void setImageConf_help() {
		String path = FileUtils.MABABY_HELPIMAGECACHAE;
		File cacheDir = StorageUtils.getOwnCacheDirectory(FS.getInstance(), path);
		DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
				.showImageForEmptyUri(R.drawable.icon)
				.bitmapConfig(Bitmap.Config.ARGB_4444)
				.showImageOnFail(R.drawable.icon).cacheInMemory(true)
				.delayBeforeLoading(8/*00*/).cacheOnDisc(true).build();
		ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
				FS.getInstance()).defaultDisplayImageOptions(defaultOptions)
				.diskCacheSize(50 * 1024 * 1024).diskCacheFileCount(1000)
				.diskCache(new UnlimitedDiskCache(cacheDir)).build();
		ImageLoader.getInstance().init(config);
//		ImageLoader.getInstance().clearDiskCache();
//		ImageLoader.getInstance().clearMemoryCache();
	}
	
	/**
	 * 调用帮助页面的时候先钓鱼调用<li>{@link #setImageConf_help()}</li>目的是修改图片路径
	 */
	public void displayHelp(String url, ImageView view){
		setImageConf_help();
		ImageLoader.getInstance().displayImage(url, view);
	}
	
	public void display(String url, ImageView view) {
		setImageConf();
		ImageLoader.getInstance().displayImage(url, view);
	}
}
